package com.dx.chat.kafka.core;

import lombok.Data;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * kafka自定义属性，可用在spel表达式bean加载
 *
 * @author wcx
 * @date 2023/6/28
 */
@Data
@ConfigurationProperties(prefix = KafkaConfig.PREFIX)
@Configuration(value = "kafkaConfig", proxyBeanMethods = false)
public class KafkaConfig extends KafkaParentProperties {

    public static final String PREFIX = "dx.kafka";

    private Boolean enable = true;

    /**
     * 自定义监听topic配置
     */
    private List<DxTopic> dxTopics = new ArrayList<>();

    @Data
    public static class DxTopic {
        /**
         * topic名字
         */
        private String name;

        /**
         * topic对应的partition数量，默认3，只能增加，不能减少，增加会触发rebalance
         */
        private Integer numPartitions = 3;
        /**
         * 副本数量，默认为1
         */
        private Short replicationFactor = 1;

        /**
         * 消费者配置
         */
        private List<Consumer> consumers = new ArrayList<>();

        public NewTopic toNewTopic() {
            return new NewTopic(this.name, this.numPartitions, this.replicationFactor);
        }

    }
}